package cn.shop.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.shop.base.MyPage;
import cn.shop.front.entity.order.SimpleOrder;
import cn.shop.front.entity.req.OrderQueryReq;
import cn.shop.front.entity.req.ReceiveReq;
import cn.shop.front.service.GameBindService;
import cn.shop.front.service.OrderService;
import cn.shop.front.service.TransactionRecordService;
import cn.shop.front.util.CurrentUserUtil;
import cn.shop.utils.ApiResult;
import cn.shop.utils.enums.GoodsType;
import cn.shop.utils.enums.OrderStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 订单查询
 *
 * @author jzw
 * @since 2021/12/23 10:15
 */
@RestController
@RequestMapping("/api/v1/order")
public class OrderController {

	@Resource
	private OrderService orderService;
	@Resource
	private GameBindService gameBindService;
	@Resource
	private TransactionRecordService recordService;

	/**
	 * 按条件获取订单列表
	 *
	 * @return 返回结果
	 */
	@GetMapping("/queryByPage")
	public ApiResult<MyPage<SimpleOrder>> list(MyPage<SimpleOrder> page, Long gameId, Long partitionId, GoodsType goodType, Integer orderByDownTime) {
		if ((gameId == null || gameId == 0L) && (partitionId == null || partitionId == 0L)) {
			return ApiResult.success(new MyPage<>());
		}
		return ApiResult.success(orderService.orderList(page, gameId, partitionId, goodType, orderByDownTime));
	}


	/**
	 * 检验当前商品是否可买
	 *
	 * @param orderId 商品id
	 * @author jzw
	 * @since 2022-01-07 15:45:19
	 */
	@SaCheckLogin
	@GetMapping("checkOrder")
	public ApiResult<Map<String, Object>> checkOrder(@RequestParam Long orderId) {
		return ApiResult.result(orderService.checkOrder(orderId));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<SimpleOrder> selectOne(@PathVariable Serializable id) {
		SimpleOrder order = orderService.getById(id);
		if (order == null || order.getStatus() != OrderStatus.PASSED) {
			return ApiResult.fail("商品不存在", null);
		}
		return ApiResult.success(order);
	}

	/**
	 * 获取订单可领取账号
	 *
	 * @param orderId 订单编号
	 * @return 返回结果
	 */
	@SaCheckLogin
	@GetMapping("/bindlist")
	public ApiResult<List<Map<String, Object>>> bindList(long orderId) {
		long userId = CurrentUserUtil.getUserId();
		SimpleOrder order = orderService.getById(orderId);
		if (order.getStatus() != OrderStatus.COMPLETED) {
			return ApiResult.fail( "订单不存在", null);
		}
		if (order.getBuyerId() != userId) {
			return ApiResult.fail("订单不存在", null);
		}
		List<Map<String, Object>> list = gameBindService.partitionBinds(userId, order.getPartitionId());
		return ApiResult.success(list);
	}

	/**
	 * 获取待领取列表
	 *
	 * @param current 第几页
	 * @param size 每页记录数
	 * @return 返回结果
	 */
	@SaCheckLogin
	@GetMapping("/unreceived")
	public ApiResult<Map<String, Object>> unreceivedList(int current, int size) {
		return ApiResult.success(orderService.unreceivedList(current, size, CurrentUserUtil.getUserId()));
	}

	/**
	 * 领取物品
	 *
	 * @param req 领取信息
	 * @return 返回结果
	 */
	@SaCheckLogin
	@PostMapping("/receive")
	public ApiResult<Map<String, Object>> receive(@Validated  @RequestBody ReceiveReq req) {
		return ApiResult.result(recordService.receive(req.getOrderId(), req.getAccount()));
	}

	/**
	 * 查看我寄卖的订单
	 *
	 * @param page 分页数据
	 * @param req 查询条件
	 * @return 返回结果
	 */
	@SaCheckLogin
	@GetMapping("/list")
	public ApiResult<MyPage<SimpleOrder>> list(MyPage<SimpleOrder> page, OrderQueryReq req) {
		long userId = CurrentUserUtil.getUserId();
		req.setSellerId(String.valueOf(userId));
		return ApiResult.success(orderService.orderList(page, req));
	}

	@SaCheckLogin
	@GetMapping("/userGame")
	public ApiResult<List<Map<String, Object>>> allBindGame() {
		long userId = CurrentUserUtil.getUserId();
		return ApiResult.success(gameBindService.userGame(userId));
	}

	@SaCheckLogin
	@GetMapping("/home")
	public ApiResult<List<SimpleOrder>> homeList() {
		long userId = CurrentUserUtil.getUserId();
		List<Map<String, Object>> userGames = gameBindService.userGame(userId);
		if (userGames.isEmpty()) {
			return ApiResult.success(Collections.emptyList());
		}
		List<Long> gameIds = userGames.stream()
				.map(g -> g.get("id"))
				.filter(Objects::nonNull)
				.map(id -> (Long) id)
				.collect(Collectors.toList());
		if (gameIds.isEmpty()) {
			return ApiResult.success(Collections.emptyList());
		}

		MyPage<SimpleOrder> page = MyPage.of(1, 20);
		page = orderService.orderListApp(page, gameIds);
		return ApiResult.success(page.getRecords());
	}

}
